package com.ama.controller;

import com.ama.dao.ApplyDao;
import com.ama.pojo.Staff;
import com.ama.service.EstateService;
import com.ama.service.StaffService;
import com.ama.service.impl.EstateServiceImpl;
import com.ama.service.impl.StaffServiceImpl;
import com.ama.utils.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class sidebarController {
    @Autowired
    private ApplyDao applyDao;


    @RequestMapping("/toApply")
    public String apply2error(HttpSession session) {
        System.out.println("toapply");
        Staff staff = (Staff) session.getAttribute("user");
        Long bedId = staff.getBedId();
        if (bedId == null) {
            return "redirect:/staff_page/s_estate.jsp";
        } else {
            return "redirect:/error/alreadyapply_error.jsp";
        }
    }

    @RequestMapping("/toCost")
    public String cost2error(HttpSession session) {
        System.out.println("toCost");
        Staff staff = (Staff) session.getAttribute("user");
        Long bedId = staff.getBedId();
        if (bedId == null) {
            return "redirect:/error/cost_error.jsp";
        } else {
            return "redirect:/staff_page/s_cost.jsp";
        }
    }


    @RequestMapping("/toApplyDetail")
    public String applyDetail2error(HttpSession session) {
        System.out.println("toApplyDetail");

        if (applyDao.selectList(null) == null) {
            return "redirect:/error/noapply_error.jsp";
        } else {
            return "redirect:/staff_page/s_applylist.jsp";
        }
    }

    @RequestMapping("/toContract")
    public String contract2error(HttpSession session) {
        System.out.println("toContract");
        Staff staff = (Staff) session.getAttribute("user");
        Long bedId = staff.getBedId();
        if (bedId == null) {
            return "redirect:/error/contract_error.jsp";
        } else {
            return "redirect:/staff_page/s_contract.jsp";
        }
    }


    @RequestMapping("/toStaffManage")
    public String manageStaff2error() {
        System.out.println("toStaffManage");
        StaffService staffService = SpringUtils.getBean(StaffServiceImpl.class);
        if (staffService.selectAll() == null) {
            return "redirect:/error/nostaff_error.jsp";
        } else {
            return "redirect:/pro_page/p_staffmanage.jsp";
        }
    }

    @RequestMapping("/toEstateManage")
    public String estateStaff2error() {
        System.out.println("toEstateManage");
        EstateService estateService = SpringUtils.getBean(EstateServiceImpl.class);
        if (estateService.selectAll() == null) {
            return "redirect:/error/noestate_error.jsp";
        } else {
            return "redirect:/pro_page/p_estatemanage.jsp";
        }
    }
}
